﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DoAnOOP.BussinessLayer;
using DevComponents.DotNetBar;

namespace DoAnOOP.NQL
{
    public partial class frmQLSV : DevComponents.DotNetBar.Office2007Form
    {
        

        #region Hiển thị
        private void frmSV_Load(object sender, EventArgs e)
        {
            clsQLSV qlsv = new clsQLSV();
            dataGridViewSV.DataSource = qlsv.LayThongTinSinhVien("");
            KiemTraChinhSua(false);
            dataGridViewSV.Columns[0].HeaderText = "MSSV";
            dataGridViewSV.Columns[1].HeaderText = "Họ";
            dataGridViewSV.Columns[2].HeaderText = "Tên";
            dataGridViewSV.Columns[3].HeaderText = "Giới tính";
            dataGridViewSV.Columns[4].HeaderText = "Ngày sinh";
            dataGridViewSV.Columns[5].HeaderText = "Nơi sinh";
            dataGridViewSV.Columns[6].HeaderText = "Khoa";
            dataGridViewSV.Columns[7].HeaderText = "Ngày vào đoàn";
            dataGridViewSV.Columns[8].HeaderText = "Nơi sinh";
           
        }
        int numRecord;
        private void dataGridViewSV_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                numRecord = e.RowIndex; 
                txtmssv.Text = dataGridViewSV.Rows[numRecord].Cells[0].Value.ToString();
                txtHo.Text = dataGridViewSV.Rows[numRecord].Cells[1].Value.ToString();
                txtTen.Text = dataGridViewSV.Rows[numRecord].Cells[2].Value.ToString();
                if (dataGridViewSV.Rows[numRecord].Cells[3].Value.ToString() == "Nam")
                    rdiNam.Checked = true;
                else
                    rdiNu.Checked = true;
                pickNgaySinh.Text = dataGridViewSV.Rows[numRecord].Cells[4].Value.ToString();
                txtNoiSinh.Text = dataGridViewSV.Rows[numRecord].Cells[5].Value.ToString();
                cboKhoa2.Text = dataGridViewSV.Rows[numRecord].Cells[6].Value.ToString();
                pickNgayvaoDoan.Text = dataGridViewSV.Rows[numRecord].Cells[7].Value.ToString();
                txtEmail.Text = dataGridViewSV.Rows[numRecord].Cells[8].Value.ToString();    
            }
            catch (Exception)
            {}
        }
        #endregion

        #region Hàm trong form
        private void KiemTraChinhSua(bool isEditting)
        {
            bntThem.Enabled = !isEditting;
            btnSua.Enabled = !isEditting;
            btnXoa.Enabled = !isEditting;
            btnXuat.Enabled = !isEditting;
            btnLuu.Enabled = isEditting;
            btnKhongluu.Enabled = isEditting;

            txtmssv.Enabled = isEditting;
            txtHo.Enabled = isEditting;
            txtTen.Enabled = isEditting;
            txtNoiSinh.Enabled = isEditting;
            txtEmail.Enabled = isEditting;
            pickNgaySinh.Enabled = isEditting;
            pickNgayvaoDoan.Enabled = isEditting;
            cboKhoa2.Enabled = isEditting;
            rdiNam.Enabled = isEditting;
            rdiNu.Enabled = isEditting;
        }

        private void Reset()
        {
            txtEmail.Text = "";
            txtHo.Text = "";
            txtmssv.Text = "";
            txtNoiSinh.Text = "";
            txtTen.Text = "";
            pickNgaySinh.Text = "";
            pickNgayvaoDoan.Text = "";
        }
        private string KiemTraLuu()
        {
            string kiemTra = "";
            if (txtmssv.Text.Equals(""))
                kiemTra += "Chưa nhập MSSV.\n";
            if (txtHo.Text.Equals(""))
                kiemTra += "Chưa nhập Họ sinh viên.\n";
            if (txtTen.Text.Equals(""))
                kiemTra += "Chưa nhập Tên sinh viên.\n";
            if (txtNoiSinh.Text.Equals(""))
                kiemTra += "Chưa nhập Nơi sinh.\n";
            if (txtEmail.Text.Equals(""))
                kiemTra += "Chưa nhập Email.";
            return kiemTra;
        }
        #endregion

        #region Sự kiện
        int flags;

        public frmQLSV()
        {
            InitializeComponent();
        }

        private void bntThem_Click(object sender, EventArgs e)
        {            
            KiemTraChinhSua(true);
            Reset();
            flags = 1;
        }
        
        private void btnKhongluu_Click(object sender, EventArgs e)
        {
            KiemTraChinhSua(false);
        }

        
        private void btnLuu_Click(object sender, EventArgs e)
        {
            clsQLSV svBUS = new clsQLSV();

            svBUS.mssv = txtmssv.Text;
            svBUS.Ho = txtHo.Text;
            svBUS.Ten = txtTen.Text;

            svBUS.Email = txtEmail.Text;
            svBUS.GioiTinh = (rdiNam.Checked == true ? "Nam" : "Nữ");
            svBUS.MaKhoa = cboKhoa2.Text;
            svBUS.NgayVaoDoan = pickNgaySinh.Text;
            svBUS.NoiSinh = txtNoiSinh.Text;

            if (KiemTraLuu().Equals(""))
            {
                switch (flags)
                {
                    case 1: //Insert
                        svBUS.ThemThongTinSinhVien();
                        break;
                    case 2: //Update
                        svBUS.UpdateThongTinSinhVien();
                        break;
                    case 3: //Delete
                        svBUS.DeleteThongTinSinhVien();
                        break;
                }
                frmSV_Load(sender, e);
                KiemTraChinhSua(false);
            }
            else
                MessageBox.Show(KiemTraLuu().ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

       
        private void btnSua_Click(object sender, EventArgs e)
        {
            flags = 2;
            string temp = txtmssv.Text;
            KiemTraChinhSua(true);
            txtmssv.Enabled = false;
            txtmssv.Text = temp; 
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if(DialogResult.OK == MessageBox.Show("Bạn có chắc rằng mình muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
            {
                flags = 3;
                btnLuu_Click(sender, e);
            }
        }
        #endregion

        private void cboKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            clsQLSV svBUS = new clsQLSV();
            string khoa;
          
            if (cboKhoa.Text == "Tất cả")
                khoa = "";
            else
                khoa = " WHERE MaKhoa = '"+cboKhoa.Text+"'";
            svBUS.LayThongTinSinhVien(khoa);
            clsQLSV qlsv = new clsQLSV();
            dataGridViewSV.DataSource = qlsv.LayThongTinSinhVien(khoa);
        }
    }
}